#pragma once


#include"proto.h"
#include<sys/types.h>
#include<sys/socket.h>
#include<unistd.h>
#include<netinet/in.h>
#include<pthread.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<iostream>


namespace TClient
{
  class TcpClient
  {
    private:
      std::string ip;
      int port;
      int sock;
    public:
      TcpClient(std::string _ip,int _port):ip(_ip),port(_port),sock(-1)
      {}
      void Init()
      {
        sock=socket(AF_INET,SOCK_STREAM,0);
        if(sock<0)
        {
          std::cerr<<"socket error"<<std::endl;
          exit(-1);
        }
      }
      void Start()
      {
        struct sockaddr_in desc;
        desc.sin_family=AF_INET;
        desc.sin_port=htons(port);
        desc.sin_addr.s_addr=inet_addr(ip.c_str());
        //发起连接
        if(connect(sock,(struct sockaddr*)&desc,sizeof(desc))<0)
        {
          std::cout<<"connect error"<<std::endl;
        }
        else{
          std::cout<<"connect success"<<std::endl;
          //输入数据
          Pra::Data data;
          std::cout<<"请输入"<<"\n\t"<<"格式为"<<" x op y"<<std::endl;
          std::cin>>data.x;
          std::cin>>data.op;
          std::cin>>data.y;

          //给服务端
          send(sock,&data,sizeof(data),0);

          //从服务端获取
          Pra::Result rsult;
          recv(sock,&rsult,sizeof(rsult),0);
          std::cout<<"server send# ";
          std::cout<<"cond is "<<rsult.code<<" result is "<<rsult.result<<std::endl;
        }
        close(sock);
      }
      ~TcpClient()
      {}
  };
}
